//
//  MPMApiClient.m
//  mompharmacy
//
//  Created by chaizhiyong on 15/5/12.
//  Copyright (c) 2015年 chaizhiyong. All rights reserved.
//

#import "CSHApiClient.h"

@implementation CSHApiClient


+ (MKNetworkEngine *)engine
{
    static dispatch_once_t onceToken;
    static MKNetworkEngine *shareEngine;
    dispatch_once(&onceToken, ^ {
        shareEngine = [[MKNetworkEngine alloc] initWithHostName:kHOST portNumber:kPORT apiPath:nil customHeaderFields:nil];

    });
    return shareEngine;
}

+ (MKNetworkOperation *)requestWithPath:(NSString *)path headers:(NSDictionary *)headers params:(NSDictionary *)params  httpMethod:(NSString*)httpMethod
{
    NSAssert(path!=nil, @"the url path can't be null");
    MKNetworkOperation *operation = [[CSHApiClient engine] operationWithPath:path params:params httpMethod:(KCNSSTRING_ISEMPTY(httpMethod))?@"POST":httpMethod ssl:NO];
    [operation setStringEncoding:NSUTF8StringEncoding];
    [operation setShouldContinueWithInvalidCertificate:YES];
  //  [operation setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
    [operation setPostDataEncoding:MKNKPostDataEncodingTypeURL];
    if (headers) {
        [operation addHeaders:headers];
    }
    
    if(params){
        [operation addParams:params];
    }
    
#ifdef DEBUG
//    CSHDLog(@"http headers=%@", [headers description]);
#endif
    return operation;
}


+ (MKNetworkOperation *)requestWithPath:(NSString *)path headers:(NSDictionary *)headers params:(NSDictionary *)params ssl:(BOOL )ssl
{
    NSAssert(path!=nil, @"the url path can't be null");
    MKNetworkOperation *operation = [[CSHApiClient engine] operationWithPath:path params:params httpMethod:@"POST" ssl:ssl];
    [operation setStringEncoding:NSUTF8StringEncoding];
    [operation setShouldContinueWithInvalidCertificate:YES];
    [operation setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
    if (headers) {
        [operation addHeaders:headers];
    }
    
    if(params){
        [operation addParams:params];
    }
    
#ifdef DEBUG
//    CSHDLog(@"http headers=%@", [headers description]);
#endif
    return operation;
}


+ (MKNetworkOperation *)requestWithUrlString:(NSString*)urlString headers:(NSDictionary *)headers params:(NSDictionary *)params  httpMethod:(NSString*)httpMethod
{
    NSAssert(urlString!=nil, @"the url path can't be null");
    
    MKNetworkOperation *operation = [[CSHApiClient engine] operationWithURLString:urlString params:params httpMethod:(KCNSSTRING_ISEMPTY(httpMethod))?@"POST":httpMethod];
    
    
    [operation setStringEncoding:NSUTF8StringEncoding];
    [operation setShouldContinueWithInvalidCertificate:YES];
    [operation setPostDataEncoding:MKNKPostDataEncodingTypeURL];
    if (headers) {
        [operation addHeaders:headers];
    }
    if(params){
        [operation addParams:params];
    }
    
#ifdef DEBUG
    //    QLKDLog(@"http headers=%@", [headers description]);
#endif
    return operation;
}

@end
